//
//  ViewController.m
//  TestWebViewPagable
//
//  Created by zhuxuhong on 2016/12/17.
//  Copyright © 2016年 zhuxh. All rights reserved.
//

#import "ViewController.h"
#import <Foundation/Foundation.h>
#import "ERCoverView.h"
#import "ERPageCurlView.h"
#import "TestView.h"

@interface ViewController ()<UIWebViewDelegate,UIScrollViewDelegate>

@end

@implementation ViewController
{
    UIWebView *_web;
    UIActivityIndicatorView *_indicator;
    UIButton *_pre;
    UIButton *_next;
    NSInteger _currentPage;
    NSInteger _totalPage;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor redColor];
//    [self testPageCurlView];
    
    [self testCoverView];
}


// 覆盖效果
-(UIView*)coverViewWithIndex: (NSInteger)index{
    UIView *v = [[UIView alloc] initWithFrame:[self frame]];
    v.backgroundColor = [UIColor greenColor];
    
    UILabel *label = [self label];
    label.text = [NSString stringWithFormat:@"Hello %ld",index];
    [v addSubview:label];
    v.userInteractionEnabled = false;
    return v;
}

-(void)testCoverView{
    NSMutableArray *covers = [NSMutableArray new];
    for (int i=0; i<5; i++) {
        [covers addObject:[self coverViewWithIndex:i]];
    }
    ERCoverView *cover = [[ERCoverView alloc] initWithFrame:[self frame] contentViews:covers];
    cover.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    cover.currentPageIndex = 0;
    [self.view addSubview:cover];
}

-(CGRect)frame{
    CGRect frame = self.view.bounds;
    frame.origin.x = 10;
    frame.origin.y = 30;
    frame.size.width -= 20;
    frame.size.height -= 40;
    return frame;
}

-(UILabel*)label{
    UILabel *label = [[UILabel alloc] initWithFrame:self.view.bounds];
    label.text = @"Hello";
    label.font = [UIFont systemFontOfSize:100];
    label.textAlignment = NSTextAlignmentCenter;
    return label;
}

// 翻页效果
-(void)testPageCurlView{
    NSMutableArray *covers = [NSMutableArray new];
    for (int i=0; i<5; i++) {
        [covers addObject:[self coverViewWithIndex:i]];
    }
    ERPageCurlView *curl = [[ERPageCurlView alloc] initWithFrame:[self frame] contentViews:covers];
    curl.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    curl.currentPageIndex = 0;
    [self.view addSubview:curl];
}


-(void)setupReaderView{
    
    _web = [[UIWebView alloc] initWithFrame:CGRectZero];
    _web.opaque = NO;
    _web.backgroundColor = [UIColor clearColor];
    _web.paginationMode = UIWebPaginationModeLeftToRight;
    _web.paginationBreakingMode = UIWebPaginationBreakingModeColumn;
    _web.scrollView.pagingEnabled = true;
    _web.scrollView.bounces = false;
    _web.scrollView.delegate = self;
    _web.scalesPageToFit = true; //这样使得pageCount准确些
    //    _web.gapBetweenPages = 40;
    _web.scrollView.clipsToBounds = true;
    //    _web.pageLength = CGRectGetWidth(self.view.bounds)/2 - 42.5; //可实现多列展示效果
    [self.view addSubview:_web];
    _web.delegate = self;
    
    
    
    _indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [self.view addSubview:_indicator];
    
    _pre = [UIButton buttonWithType:UIButtonTypeSystem];
    _pre.backgroundColor = [UIColor greenColor];
    [_pre setTitle:@"上一页" forState:UIControlStateNormal];
    _pre.enabled = false;
    
    _next = [UIButton buttonWithType:UIButtonTypeSystem];
    _next.backgroundColor = [UIColor greenColor];
    [_next setTitle:@"下一页" forState:UIControlStateNormal];
    _next.enabled = false;
    
    [_pre addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventTouchUpInside];
    [_next addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_pre];
    [self.view addSubview:_next];
    
    [self loadContentForCalculateTotalPage];
}

-(void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    
//    CGRect frame = self.view.bounds;
//    frame.origin.x = 10;
//    frame.origin.y = 30;
//    frame.size.width -= 20;
//    frame.size.height -= 40;
//    _web.frame = frame;
//    
//    _pre.frame = CGRectMake(0, self.view.center.y-50, 100, 100);
//    _next.frame = CGRectMake(CGRectGetWidth(self.view.bounds)-100, self.view.center.y-50, 100, 100);
//
//    _indicator.center = self.view.center;
}

-(void)changePage: (UIButton*)sender{
    if (sender == _pre) {
        if (_currentPage > 0) {
            _currentPage -= 1;
        }
    }
    else{
        if (_currentPage < _totalPage-1) {
            _currentPage += 1;
        }
    }
    [self toCurrentPage];
//    _web.pageLength = _currentPage;
}

-(void)toCurrentPage{
    [UIView animateWithDuration:0.5 animations:^{
        _web.scrollView.contentOffset = CGPointMake(_currentPage*(CGRectGetWidth(_web.bounds)+_web.gapBetweenPages), 0);
    }];
}

-(void)loadContentForCalculateTotalPage{
    [_indicator startAnimating];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"demo.html" ofType:nil];
    //    NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
    NSString *data = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    [_web loadHTMLString:data baseURL:nil];
    //    [_web loadRequest:[NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"demo.html" withExtension:nil]]];
}

-(void)webViewDidStartLoad:(UIWebView *)webView{
//    [_indicator startAnimating];
    /*
     When paginationMode is right to left or left to right, this property represents the width of each page. When paginationMode is top to bottom or bottom to top, this property represents the height of each page.
     
     The default value is 0, which means the layout uses the size of the viewport to determine the dimensions of the page. Adjusting the value of this property causes a relayout.
     
     */
    
//    _web.pageLength = webView.bounds.size.width;
}

-(void)webViewDidFinishLoad:(UIWebView *)webView{
    CGSize size = webView.scrollView.contentSize;
    if (size.width == 0) {
        return;
    }
    _pre.enabled = true;
    _next.enabled = true;
    
    _totalPage = webView.pageCount;
    NSLog(@"size: %@ pageCount: %d",NSStringFromCGSize(size), _totalPage);
    
    //重设contentSize
    size.width = _totalPage * CGRectGetWidth(webView.bounds);
//    _web.scrollView.contentSize = size;
    NSLog(@"resize: %@",NSStringFromCGSize(size));
    
    [_indicator stopAnimating];
    
    [self toCurrentPage];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    _currentPage = scrollView.contentOffset.x / CGRectGetWidth(_web.bounds);
    NSLog(@"current page: %d",_currentPage);
}

-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    
    [self loadContentForCalculateTotalPage];
}

@end
